//
//  FileManager.m
//  Mehfile Zainab
//
//  Created by Sitara Badourali on 02/05/12.
//  Copyright 2012 Mehfile Zainab. All rights reserved.
//

#import "FileManager.h"


@implementation FileManager

- (NSString *)OpenDataFile:(NSString *) plistFile {
	
    NSLog(@"Open file named : %@",plistFile);
    
	NSArray *fileNameSplit = [plistFile componentsSeparatedByString:@"."];
	NSString *fileName = [fileNameSplit objectAtIndex:0];
	NSString *bundle = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];
	
    NSLog(@"Bundle name for file : %@",bundle);
    
	return bundle;
	
}

+ (NSString *) getCachedDataFile {
    //NSLog(@"Get the path of cached data file");
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MZCache" ofType:@"plist"];
    //NSLog(@"Value of path : %@",path);
    NSArray *URLArray;
    if (path) {
        URLArray = [[NSArray alloc] initWithContentsOfFile:path];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *dataPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"MZCache"];
        //NSLog(@"Value of dataPath : %@",dataPath);
        //NSLog(@"Value of URL : %@",[URLArray objectAtIndex:0]);
        NSString *fileName = [[URLArray objectAtIndex:0] lastPathComponent];
        //NSLog(@"Value of filename : %@",fileName);
        return [[dataPath stringByAppendingPathComponent:fileName] retain];
    } else {
        NSLog(@"Error when acceding to the property list file");
        return nil;
    }

}

+ (NSString *) getCachedDataFile:(NSString *)URLString {
    //NSLog(@"Get the path of cached data file corresponding to URL : %@",URLString);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *dataPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"MZCache"];
    //NSLog(@"Value of dataPath : %@",dataPath);
    NSString *fileName = [URLString lastPathComponent];
    //NSLog(@"Value of filename : %@",fileName);
    return [[dataPath stringByAppendingPathComponent:fileName] retain];
}


@end
